﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Net.Mail;

namespace Adams.Services.Email
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionstring = Shared.Config.GetAppSetting("ConnectionString");
            string server = Shared.Config.GetAppSetting("Server");
            string username = Shared.Config.GetAppSetting("Username");
            string password = Shared.Config.GetAppSetting("Password");

            using(SmtpClient SMTPServer = new SmtpClient(server))
            {
                using (SqlConnection connection = new SqlConnection(connectionstring))
                {
                    using (SqlCommand command = new SqlCommand(@"SELECT * FROM EmailQueue WHERE IsProcessed = 0", connection))
                    {
                        connection.Open();
                        using (SqlDataReader reader = command.ExecuteReader())
                        {
                            if (reader.HasRows)
                            {
                                while (reader.Read())
                                {
                                    int emailid = reader["EmailID"].ToInt();
                                    string subject = reader["Subject"].ToString();
                                    string from = reader["From"].ToString();
                                    string to = reader["To"].ToString();
                                    int priority = reader["Priority"].ToInt();
                                    string body = reader["Body"].ToString();

                                    if (Shared.Validation.Email(to))
                                    {
                                        MailMessage mailObj = new MailMessage(from, to, subject, body);
                                        mailObj.IsBodyHtml = true;
                                        SMTPServer.Send(mailObj);

                                        string query = @"UPDATE EmailQueue SET IsProcessed = 1 WHERE EmailID = @EmailID";
                                        using (SqlConnection connection2 = new SqlConnection(connectionstring))
                                        {
                                            connection2.Open();
                                            using (SqlCommand cmd = new SqlCommand(query, connection2))
                                            {
                                                cmd.Parameters.Add(new SqlParameter("@EmailID", emailid));
                                                cmd.ExecuteNonQuery();
                                            }
                                            connection2.Close();
                                        }
                                    }
                                }
                            }
                            reader.Close();
                            connection.Close();
                        }
                    }
                }
            }
        }
    }
}
